(function () {

	$(function () {

		packager('app.config');

		app.config = new function () {

			var exports = {};

			// init app
			exports.init = function () {

				exports.initLodashConfig();
				exports.initSprintf();
				exports.enhanceJqueryValidation();
				exports.customizeJqueryValidationTranslation();
				exports.initBootstrap();
				exports.initSweetAlert();
				exports.initFoucPatch();

			};

			exports.initLodashConfig = initLodashConfig;
			exports.initSprintf = initSprintf;
			exports.enhanceJqueryValidation = enhanceJqueryValidation;
			exports.customizeJqueryValidationTranslation = customizeJqueryValidationTranslation;
			exports.initBootstrap = initBootstrap;
			exports.initSweetAlert = initSweetAlert;
			exports.initFoucPatch = initFoucPatch;

			return exports;
		};
	});


	// config lodash template engine
	// 配置 lodash template 模版引擎, 考虑放到 layout 里
	function initLodashConfig() {
		_.templateSettings.evaluate = /\[\[(.+?)\]\]/g;
		_.templateSettings.interpolate = /\[\[=(.+?)\]\]/g;
		_.templateSettings.escape = /\[\[-(.+?)\]\]/g;
	}


	// --------------------------------------------------------------
	// 前端表单验证错误信息翻译宏文件配置
	// 用法: $.sprintf('required', 'email');
	// 返回: "邮箱不能为空"
	// 只翻译的话用法如: $.trans('http.404');
	// 返回: 未找到
	function initSprintf() {

		// 查看所有翻译
		$.phrases = function () {
			return Phrases;
		};

		// 翻译, 第一个参数为宏标记, 之后所有都为向宏值里插入的变量
		$.sprintf = function () {

			var str = Array.prototype.slice.call(arguments, 0, 1)[0];
			var args = Array.prototype.slice.call(arguments, 1);

			// 尝试翻译参数
			args = $.map(args, function (arg) {
				try {
					if (arg in Phrases.attributes) {
						return Phrases.attributes[arg];
					} else {
						return arg;
					}
				} catch (e) {
					return arg;
				}
			});

			var translation = index(Phrases, str);

			// 把翻译插到开头
			args.unshift(translation);

			try {
				return sprintf.apply(null, args);
			} catch (e) {
				console.log('翻译出错, 检查代码');
				console.log(e);
				console.log(Phrases);
			}
		};

		$.trans = $.sprintf;

		// 以点引用(字符串)的方式从对像里取出对应值
		function index(obj, is, value) {
			if (typeof is == 'string')
				return index(obj, is.split('.'), value);
			else if (is.length == 1 && value !== undefined)
				return obj[is[0]] = value;
			else if (is.length == 0)
				return obj;
			else
				return index(obj[is[0]], is.slice(1), value);
		}
	}


	// --------------------------------------------------------------
	// 为 jquery validation 增加文件大小验证功能, 仅支持高级浏览器
	function enhanceJqueryValidation() {
		$.validator.addMethod('filesize', function (value, element, param) {
			// param = size (in bytes)
			// element = element to validate (<input>)
			// value = value of the element (file name)

			// no file
			if (!element.files[0]) {
				return true;
			}
			console.log($(element).attr('name') + ' filesize:', element.files[0].size / 1024);
			return this.optional(element) || (element.files[0].size / 1024 <= param)
		}, $.validator.format("文件大小应小于 {0} KB"));

		$.validator.addMethod('requiredwithout', function (value, element, param) {

			if (!!$(element).val() || !!$(param).val()) {
				$(param).closest('.form-group').removeClass('has-error').find('label.error').addClass('hidden');
			} else {
				$(param).closest('.form-group').addClass('has-error').find('label.error').removeClass('hidden');
			}

			return !!$(element).val() || !!$(param).val();
		});
	}

	// --------------------------------------------------------------
	// super jquery validation translation; sync with backend
	function customizeJqueryValidationTranslation() {

		// 使用一些后端的翻译
		$.validator.messages.required = function (param, input) {
			return $.trans('validation.required', $.trans('validation.attributes.' + input.name));
		};

		$.validator.messages.maxlength = function (param, input) {
			return $.trans('validation.max.string', $.trans('validation.attributes.' + input.name), $(input).data('rule-maxlength'));
		};

		$.validator.messages.minlength = function (param, input) {
			return $.trans('validation.min.string', $.trans('validation.attributes.' + input.name), $(input).data('rule-minlength'));
		};

		// 至少得填一个
		$.validator.messages.requiredwithout = function (param, input) {
			return $.trans('validation.required_without', $.trans('validation.attributes.' + input.name), $.trans('validation.attributes.' + $($(input).data('rule-requiredwithout')).get(0).name));
		};
	}

	// --------------------------------------------------------------
	// bootstrap config
	function initBootstrap() {

		// init bootstrap tooltip
		$('[data-toggle="tooltip"]').tooltip();
	}


	// --------------------------------------------------------------
	// sweet alert translation
	function initSweetAlert() {
		swal.setDefaults({
			confirmButtonText: '确定',
			cancelButtonText: '取消',
			animation: false
		});
	}

	// --------------------------------------------------------------
	// usage: add class 'fouc' to element you want to prevent fouc effect
	function initFoucPatch() {

		setTimeout(function () {
			$('.fouc').removeClass('fouc');

			// display submit/save button when js finishes
			$('.btn-submit, .btn-save').css('opacity', 1);
		}, 0);
	}

})();



